package se.perp.c4.ui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import se.perp.c4.control.iface.ITerritorySelectionListener;
import se.perp.c4.model.Territory.ETerritory;

public class TerritorySelectionPanel extends JPanel implements
        MouseMotionListener, MouseListener {

    private final ITerritorySelectionListener listener;
    private final Iterable<ETerritory> selectable;
    private final BufferedImage bgImage;
    private ETerritory hoveredTerritory = null;
    private final Color green = new Color(0, 255, 0, 96);
    private final Color greenHi = new Color(0, 255, 0, 192);

    public TerritorySelectionPanel(
            Container toClone,
            ITerritorySelectionListener l,
            Iterable<ETerritory> selectable) {
        listener = l;
        this.selectable = selectable;

        bgImage = new BufferedImage(toClone.getWidth(), toClone.getHeight(), BufferedImage.TYPE_INT_RGB);
        toClone.paintAll(bgImage.getGraphics());

        setLayout(null);
        setSize(bgImage.getWidth(), bgImage.getHeight());
        setPreferredSize(getSize());

        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {

        ETerritory over = RegionBounds.TerritoryIdAt(e.getPoint());

        if (over == hoveredTerritory) {
            return;
        }

        List<Rectangle> repaintRects = new ArrayList<Rectangle>();
        if (hoveredTerritory != null) {
            repaintRects.add(RegionBounds.get(hoveredTerritory).getBounds());
        }

        hoveredTerritory = null;
        for (ETerritory et : selectable) {
            if (et == over) {
                hoveredTerritory = over;
                repaintRects.add(RegionBounds.get(over).getBounds());
                break;
            }
        }
        for (Rectangle r : repaintRects) {
            repaint(r);
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        for (ETerritory et : selectable) {
            if (et == hoveredTerritory) {
                listener.territorySelected(hoveredTerritory);
                break;
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bgImage, 0, 0, null);

        for (ETerritory et : selectable) {
            Polygon poly = RegionBounds.get(et);
            Color c = green;
            if (et == hoveredTerritory) {
                c = greenHi;
            }
            g.setColor(c);
            g.fillPolygon(poly);
        }
    }
}
